<% delete_perm = has_permission?("Postgres:delete", @pg) %>

<% if @edit_perm %>
  <div class="p-6">
    <div class="md:flex md:items-center md:justify-between pb-2 lg:pb-4">
      <div class="min-w-0 flex-1">
        <h3 class="text-2xl font-bold leading-7 text-gray-900 sm:truncate sm:text-2xl sm:tracking-tight">
          Maintenance Window
        </h3>
      </div>
    </div>
    <div class="overflow-hidden rounded-lg shadow ring-1 ring-black ring-opacity-5 bg-white divide-y divide-gray-200">
      <div class="px-4 py-5 sm:p-6">
        <% form(action: "#{path(@pg)}/set-maintenance-window", method: :post) do %>
          <div class="space-y-4">
            <div class="grid grid-cols-12 gap-6">
              <div class="col-span-12 sm:col-span-7">
                <%== part(
                    "components/form/select",
                    name: "maintenance_window_start_at",
                    placeholder: "No Maintenance Window",
                    options:
                      (0..23).map do
                        [it, "#{"%02d" % it}:00 - #{"%02d" % ((it + PostgresResource::MAINTENANCE_DURATION_IN_HOURS) % 24)}:00 (UTC)"]
                      end,
                    label: "Maintenance Window:",
                    selected: @pg.maintenance_window_start_at
                  ) %>
              </div>
              <div class="col-span-12 sm:col-span-5 flex justify-end items-end">
                <%== part("components/form/submit_button", text: "Set") %>
              </div>
            </div>
          </div>
        <% end %>
      </div>
    </div>
  </div>
<% end %>
<!-- Danger Zone -->
<% if @edit_perm || delete_perm %>
  <div class="p-6">
    <div class="md:flex md:items-center md:justify-between pb-2 lg:pb-4">
      <div class="min-w-0 flex-1">
        <h3 class="text-2xl font-bold leading-7 text-gray-900 sm:truncate sm:text-2xl sm:tracking-tight">
          Danger Zone
        </h3>
      </div>
    </div>
    <div class="overflow-hidden rounded-lg shadow ring-1 ring-black ring-opacity-5 bg-white divide-y divide-gray-200">
      <!-- Promote -->
      <% if @edit_perm && @pg.read_replica? %>
        <div class="px-4 py-5 sm:p-6">
          <% form(action: "#{path(@pg)}/promote", method: :post) do %>
            <div class="sm:flex sm:items-center sm:justify-between">
              <div>
                <h3 class="text-base font-semibold leading-6 text-gray-900">Promote PostgreSQL read replica database</h3>
                <div class="mt-2 text-sm text-gray-500">
                  <p>This action will promote and restart the PostgreSQL database. The database will stop replicating
                    permanently. It will be offline momentarily, and all connections will be dropped.</p>
                </div>
              </div>
              <div id="postgres-replica-promote-<%=@pg.ubid %>" class="mt-5 sm:ml-6 sm:mt-0 sm:flex sm:flex-shrink-0 sm:items-center">
                <div class="col-span-12 sm:col-span-2 flex justify-end items-end">
                  <%== part("components/form/submit_button", text: "Promote", extra_class: "promote-btn") %>
                </div>
              </div>
            </div>
          <% end %>
        </div>
      <% end %>
      <!-- Reset password -->
      <% if @edit_perm && !@pg.read_replica? %>
        <div class="px-4 py-5 sm:p-6">
          <% form(action: "#{path(@pg)}/reset-superuser-password", method: :post) do %>
            <div class="space-y-4">
              <div>
                <h3 class="text-base font-semibold leading-6 text-gray-900">Reset superuser password</h3>
              </div>
              <div class="grid grid-cols-12 gap-6">
                <div class="col-span-12 sm:col-span-5">
                  <%== part(
                      "components/form/text",
                      label: "New password",
                      name: "password",
                      type: "password",
                      attributes: {
                        required: true
                      },
                      extra_class: "reset-superuser-password-new-password"
                    ) %>
                </div>
                <div class="col-span-12 sm:col-span-5">
                  <%== part(
                      "components/form/text",
                      label: "New password (repeat)",
                      name: "repeat_password",
                      type: "password",
                      attributes: {
                        required: true
                      },
                      extra_class: "reset-superuser-password-new-password-repeat"
                    ) %>
                </div>
                <div class="col-span-12 sm:col-span-2 flex justify-end items-end">
                  <%== part("components/form/submit_button", text: "Reset") %>
                </div>
              </div>
            </div>
          <% end %>
        </div>
      <% end %>
      <!-- Restart Card -->
      <% if @edit_perm %>
        <div class="px-4 py-5 sm:p-6">
          <% form(action: "#{path(@pg)}/restart", method: :post) do %>
            <div class="sm:flex sm:items-center sm:justify-between">
              <div>
                <h3 class="text-base font-semibold leading-6 text-gray-900">Restart PostgreSQL database</h3>
                <div class="mt-2 text-sm text-gray-500">
                  <p>This action will restart the PostgreSQL database. The database will be offline momentarily, and all
                    connections will be dropped.</p>
                </div>
              </div>
              <div id="postgres-restart-<%=@pg.ubid %>" class="mt-5 sm:ml-6 sm:mt-0 sm:flex sm:flex-shrink-0 sm:items-center">
                <div class="col-span-12 sm:col-span-2 flex justify-end items-end">
                  <%== part("components/form/submit_button", text: "Restart", extra_class: "restart-btn") %>
                </div>
              </div>
            </div>
          <% end %>
        </div>
        <div class="px-4 py-5 sm:p-6">
          <% form(action: "#{path(@pg)}/rename", method: :post) do %>
            <%== part("components/rename_object_input", object: @pg, type: "PostgreSQL database", text: "Renaming a PostgreSQL database changes the connection info, so doing so requires updating your applications.") %>
          <% end %>
        </div>
      <% end %>
      <!-- Delete Card -->
      <% if delete_perm %>
        <div class="px-4 py-5 sm:p-6">
          <div class="sm:flex sm:items-center sm:justify-between">
            <div>
              <h3 class="text-base font-semibold leading-6 text-gray-900">Delete PostgreSQL database</h3>
              <div class="mt-2 text-sm text-gray-500">
                <p>This action will permanently delete this PostgreSQL database.</p>
              </div>
            </div>
            <div id="postgres-delete-<%=@pg.ubid %>" class="mt-5 sm:ml-6 sm:mt-0 sm:flex sm:flex-shrink-0 sm:items-center">
              <%== part("components/delete_button", url: path(@pg), confirmation: @pg.name, redirect: "#{@project.path}/postgres") %>
            </div>
          </div>
        </div>
      <% end %>
    </div>
  </div>
<% end %>
